# 6.11 Standalone Use of Enjoy

Enjoy Template Engine is not limited to web applications and can be used in any Java development environment. It is often used for code generation, email generation, template message generation, and other scenarios that require template-based data. It is extremely easy to use.

Due to the demand for using Enjoy in non-JFinal environments, an independent version of the Enjoy Template Engine has been released. The Maven coordinates are as follows:

<dependency>
  <groupId>com.jfinal</groupId>
  <artifactId>enjoy</artifactId>
  <version>5.1.2</version>
</dependency>
1
2
3
4
5

You can include Enjoy in any Java project as a standalone dependency.

# 1. Engine and Template

The Engine serves as the configuration and usage entry point for Enjoy. One of its main functions is to configure various parameters for Enjoy. Another is to obtain a Template object through the getTemplate and getTemplateByString methods, such as:

Engine engine = Engine.use();
 
engine.setDevMode(true);
engine.setToClassPathSourceFactory();
 
engine.getTemplate("index.html");
1
2
3
4
5
6

The Template object abstracts the template and allows you to render it using the render series of methods. For example:

Kv kv = Kv.by("key", 123);
Template template = engine.getTemplate("index.html");
 
// Render to OutputStream in byte stream mode
ByteArrayOutputStream baos = new ByteArrayOutputStream();
template.render(kv, baos);
 
// Render to StringWriter in character stream mode
StringWriter sw = new StringWriter();
template.render(kv, sw);
 
// Render directly to a String variable
String str = template.renderToString(kv);
1
2
3
4
5
6
7
8
9
10
11
12
13

# 2. Basic Usage

Here's an example to illustrate:

Engine.use().getTemplate("demo.html").renderToString(Kv.by("k", "v"));
1

In just one line of code, you can use the template engine in any environment.

# 3. Advanced Usage

Here's an example:

Engine engine = Engine.create("myEngine");
engine.setDevMode(true);
engine.setToClassPathSourceFactory();
Template template = engine.getTemplate("wxAppMsg.txt");
String wxAppMsg = template.renderToString(Kv.by("toUser", "james"));
 
engine = Engine.use("myEngine");
1
2
3
4
5
6
7

This example shows how to create an Engine object named "myEngine", set it for hot template file reloading, and configure the engine to load template files from the class path and jar files.

# 4. Engine Object Management

There are two ways to create an Engine object: one is through the Engine.create(name) method, and the other is using the new Engine() statement. Objects created by the former are managed by the Engine module and can be retrieved using Engine.use(name). In contrast, objects created by the latter are not managed by the Engine module and cannot be retrieved in this way. Developers need to manage these objects themselves.

The design of Engine object management allows multiple Engine instances to be used for different purposes within the same application. JFinal's own render and activerecord modules are typical examples of this usage.

It's strongly recommended to join the JFinal Club to gain access to a comprehensive set of best practices and example projects. This will help you master the simplest ways to use the JFinal Template Engine, saving you the time you would spend reading the documentation. JFinal Club Website (opens new window)

Last Updated: 9/22/2023, 4:50:57 AM